﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.ComponentModel.DataAnnotations;
using WalkingTec.Mvvm.Core;
using WalkingTec.Mvvm.Core.Extensions;
using nERP.Model.So;
using nERP.Model;
using nERP.Model.Bas;
using nERP.Model.Wms;

namespace nERP.ViewModel.So.SaleReturnVMs
{
    public partial class SaleReturnListVM : BasePagedListVM<SaleReturn_View, SaleReturnSearcher>
    {

        protected override IEnumerable<IGridColumn<SaleReturn_View>> InitGridHeader()
        {
            return new List<GridColumn<SaleReturn_View>>{

                this.MakeGridHeader(x => x.SaleReturn_SaleReturnCode).SetTitle(@Localizer["Page.退货单号"].Value),
                this.MakeGridHeader(x => x.SaleReturn_SaleOutWarehouse).SetTitle(@Localizer["Page.销售出库"].Value),
                this.MakeGridHeader(x => x.SaleReturn_Sale).SetTitle(@Localizer["Page.销售单号"].Value),
                this.MakeGridHeader(x => x.SaleReturn_Customer).SetTitle(@Localizer["Page.客户"].Value),
                this.MakeGridHeader(x => x.SaleReturn_InvoicesDate).SetTitle(@Localizer["Page.单据日期"].Value),
                this.MakeGridHeader(x => x.SaleReturn_MakeUser).SetTitle(@Localizer["Page.制单人"].Value),
                this.MakeGridHeader(x => x.SaleReturn_Status).SetTitle(@Localizer["Page.单据状态"].Value),
                this.MakeGridHeader(x => x.SaleReturn_Remark).SetTitle(@Localizer["_Admin.Remark"].Value),
                this.MakeGridHeader(x => x.SaleReturn_TotalPrice).SetTitle(@Localizer["Page.总金额"].Value),
                this.MakeGridHeader(x => x.SaleReturn_TotalPrice1).SetTitle(@Localizer["Page.已结金额"].Value),
                this.MakeGridHeader(x => x.SaleReturn_TotalPrice2).SetTitle(@Localizer["Page.未结金额"].Value),
                this.MakeGridHeader(x => x.SaleReturn_CreateTime).SetTitle(@Localizer["_Admin.CreateTime"].Value),
                this.MakeGridHeader(x => x.SaleReturn_UpdateTime).SetTitle(@Localizer["_Admin.UpdateTime"].Value),
                this.MakeGridHeader(x => x.SaleReturn_CreateBy).SetTitle(@Localizer["_Admin.CreateBy"].Value),
                this.MakeGridHeader(x => x.SaleReturn_UpdateBy).SetTitle(@Localizer["_Admin.UpdateBy"].Value),
                this.MakeGridHeader(x => x.SaleReturn_IsValid).SetTitle(@Localizer["_Admin.IsValid"].Value),
                this.MakeGridHeader(x => "wtmtemp_CanApprove").SetHide().SetFormat((a, b) =>
                {
                    if(a.wtmtemp_CanApprove) { return "true"; }
                    else { return "false"; }
                }),


                this.MakeGridHeaderAction(width: 200)
            };
        }


        public override IOrderedQueryable<SaleReturn_View> GetSearchQuery()
        {
            var query = DC.Set<SaleReturn>()

                .CheckContain(Searcher.SaleReturnCode, x => x.SaleReturnCode)
                .CheckEqual(Searcher.SaleOutWarehouseId, x => x.SaleOutWarehouseId)
                .CheckEqual(Searcher.SaleId, x => x.SaleId)
                .CheckEqual(Searcher.CustomerId, x => x.CustomerId)
                .CheckBetween(Searcher.InvoicesDate?.GetStartTime(), Searcher.InvoicesDate?.GetEndTime(), x => x.InvoicesDate, includeMax: false)
                .CheckEqual(Searcher.MakeUserId, x => x.MakeUserId)
                .CheckEqual(Searcher.Status, x => x.Status)
                .CheckContain(Searcher.Remark, x => x.Remark)
                .CheckEqual(Searcher.TotalPrice, x => x.TotalPrice)
                .CheckEqual(Searcher.TotalPrice1, x => x.TotalPrice1)
                .CheckEqual(Searcher.TotalPrice2, x => x.TotalPrice2)
                .CheckBetween(Searcher.CreateTime?.GetStartTime(), Searcher.CreateTime?.GetEndTime(), x => x.CreateTime, includeMax: false)
                .CheckBetween(Searcher.UpdateTime?.GetStartTime(), Searcher.UpdateTime?.GetEndTime(), x => x.UpdateTime, includeMax: false)
                .CheckContain(Searcher.CreateBy, x => x.CreateBy)
                .CheckContain(Searcher.UpdateBy, x => x.UpdateBy)
                .Select(x => new SaleReturn_View
                {
                    ID = x.ID,

                    SaleReturn_SaleReturnCode = x.SaleReturnCode,
                    SaleReturn_SaleOutWarehouse = DC.Set<SaleOutWarehouse>().Where(z0 => z0.SaleOutWarehouseCode == x.SaleOutWarehouseId).Select(y => y.SaleOutWarehouseCode).FirstOrDefault(),
                    SaleReturn_Sale = DC.Set<Sale>().Where(z0 => z0.SaleCode == x.SaleId).Select(y => y.SaleCode).FirstOrDefault(),
                    SaleReturn_Customer = DC.Set<Customer>().Where(z0 => z0.CustomerCode == x.CustomerId).Select(y => y.CustomerName).FirstOrDefault(),
                    SaleReturn_InvoicesDate = x.InvoicesDate,
                    SaleReturn_MakeUser = DC.Set<FrameworkUser>().Where(z0 => z0.ITCode == x.MakeUserId).Select(y => y.Name).FirstOrDefault(),
                    SaleReturn_Status = x.Status,
                    SaleReturn_Remark = x.Remark,
                    SaleReturn_TotalPrice = x.TotalPrice,
                    SaleReturn_TotalPrice1 = x.TotalPrice1,
                    SaleReturn_TotalPrice2 = x.TotalPrice2,
                    SaleReturn_CreateTime = x.CreateTime,
                    SaleReturn_UpdateTime = x.UpdateTime,
                    SaleReturn_CreateBy = x.CreateBy,
                    SaleReturn_UpdateBy = x.UpdateBy,
                    SaleReturn_IsValid = x.IsValid,
                })
                .OrderBy(x => x.ID);
            return query;
        }


        public override void AfterDoSearcher()
        {
            if (Wtm.LoginUserInfo != null)
            {
                var ids = GetMyApproves();
                foreach (var item in EntityList)
                {
                    var m = ids.Find(x => x.ModelID == item.GetID().ToString());
                    if (m != null)
                    {
                        item.wtmtemp_CanApprove = true;
                    }
                }
            }
        }


    }
    public class SaleReturn_View : SaleReturn
    {

        public string SaleReturn_SaleReturnCode { get; set; }
        public string SaleReturn_SaleOutWarehouse { get; set; }
        public string SaleReturn_Sale { get; set; }
        public string SaleReturn_Customer { get; set; }
        public DateTime? SaleReturn_InvoicesDate { get; set; }
        public string SaleReturn_MakeUser { get; set; }
        public StatusTypeEnum? SaleReturn_Status { get; set; }
        public string SaleReturn_Remark { get; set; }
        public decimal? SaleReturn_TotalPrice { get; set; }
        public decimal? SaleReturn_TotalPrice1 { get; set; }
        public decimal? SaleReturn_TotalPrice2 { get; set; }
        public DateTime? SaleReturn_CreateTime { get; set; }
        public DateTime? SaleReturn_UpdateTime { get; set; }
        public string SaleReturn_CreateBy { get; set; }
        public string SaleReturn_UpdateBy { get; set; }
        public bool SaleReturn_IsValid { get; set; }
        public bool wtmtemp_CanApprove { get; set; }

    }

}